/** 计算函数
 * @ 标  识: hchhIOT
 * @ 作  者: 阿拉丁矿工 黄传鸿 <hchhyzc@foxmail.com>
 */

/* ================================================================================================ */
// 🍀引入
/* ================================================================================================ */

/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// 🐸 定义
/**~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
type CoordinateType = {
  x: number;
  y: number;
};


/* ================================================================================================ */
//                                函数
/* ================================================================================================ */
//

/** 获取位置 
 * @参数 StartPoint 开始点    {x,y}
 * @参数 EndPoint   结束点    {x,y}
 * @返回 Places   位置   {tl tc tr l c r bl bc br}
*/
export const onGetPlaces = (StartPoint: CoordinateType, EndPoint: CoordinateType): any => {
 // console.log("线计算函数-获取位置");

  /** 上左点 */
  let TopLeftPoint = {
    x: Number(StartPoint.x),
    y: Number(StartPoint.y),
  }

  /** 上中点 */
  let TopCenterPoint = {
    x: Number((EndPoint.x - StartPoint.x) / 2),
    y: Number(StartPoint.y),
  }

  /** 上右点 */
  let TopRightPoint = {
    x: Number(EndPoint.x),
    y: Number(StartPoint.y),
  }

  /** 左点 */
  let LeftPoint = {
    x: Number(StartPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 中心点 */
  let MiddlePoint = {
    x: Number((EndPoint.x - StartPoint.x) / 2),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 左点 */
  let RightPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 下左点 */
  let BottomLeftPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 下中点 */
  let BottomCenterPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 下右点 */
  let BottomRightPoint = {
    x: Number(EndPoint.x),
    y: Number((EndPoint.y - StartPoint.y) / 2),
  }
  /** 位置 */
  let Places = {
    tl: TopLeftPoint,
    tc: TopCenterPoint,
    tr: TopRightPoint,
    l: LeftPoint,
    m: MiddlePoint,
    r: RightPoint,
    bl: BottomLeftPoint,
    bc: BottomCenterPoint,
    br: BottomRightPoint
  }
  return Places;
}

/** 获取线_中点 
 * @参数 StartPoint 开始点    {x,y}
 * @参数 EndPoint   结束点    {x,y}
 * @返回 MiddlePoint   中点   {x,y}
*/
export const onGetLineMiddle = (StartPoint: CoordinateType, EndPoint: CoordinateType): CoordinateType => {
 // console.log("线计算函数-获取线中点");
  let MiddlePoint: CoordinateType = {
    x: 0,
    y: 0
  }
  if (EndPoint.x === StartPoint.x) {
    MiddlePoint = {
      x: Number(StartPoint.x),
      y: Number(StartPoint.y + ((EndPoint.y - StartPoint.y) / 2)),
    }
  }
  else if (EndPoint.y === StartPoint.y) {
    MiddlePoint = {
      x: Number(StartPoint.x + ((EndPoint.x - StartPoint.x) / 2)),
      y: Number(StartPoint.y),
    }
  }
  else {
    MiddlePoint = {
      x: Number(StartPoint.x + ((EndPoint.x - StartPoint.x) / 2)),
      y: Number(StartPoint.y + ((EndPoint.y - StartPoint.y) / 2)),
    }

  }
  return MiddlePoint;
}

/** 获取线_两点间的长度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Lang       长度  
 */
export const onGetLineLang = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  // 两点间的长度
  var Lang = Number(Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1)));
  return Lang;
}

/** 获取X轴的夹角_弧度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Radin      弧度
 */
export const onGetLineXRadin = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));
  return Radin;
};

/** 获取X轴的夹角_角度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Angle      角度
 */
export const onGetLineXAngle = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));

  return Radin;
}

/** 获取Y轴的夹角-弧度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Radin      弧度
 */
export const onGetLineYRadin = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));
  return Radin;
}

/** 获取Y轴的夹角-角度
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 Angle      角度
 */
export const onGetLineYAngle = (StartPoint: CoordinateType, EndPoint: CoordinateType): number => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  /** 弧度 */
  let Radin = Math.atan2((y1 - y2), (x2 - x1));

  return Radin;
}

/** 获取两点间圆角参数
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 info   中点 [x,y]
 */
export const onGetLineInfo = (StartPoint: CoordinateType, EndPoint: CoordinateType, Angle: number) => {
  let x1 = StartPoint.x;
  let y1 = StartPoint.y;
  let x2 = EndPoint.x;
  let y2 = EndPoint.y;
  let PI = Math.PI;
  // 两点间的x轴夹角弧度
  let xAngle = Math.atan2((y2 - y1), (x2 - x1));
  // 转为角度
  xAngle = 360 * xAngle / (2 * PI);
  // 两点间的长度
  let L = Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
  // 计算等腰圆分多边形斜边长度
  let L2 = L / 2 / Math.cos(Angle * 2 * PI / 360);
  // 求第一个顶点坐标，位于下边
  let val1 = [];
  // 求第二个顶点坐标，位于上边
  let val2 = [];
  val1[0] = x1 + Math.round(L2 * Math.cos((xAngle + Angle) * 2 * PI / 360));
  val1[1] = y1 + Math.round(L2 * Math.sin((xAngle + Angle) * 2 * PI / 360));
  val2[0] = x1 + Math.round(L2 * Math.cos((xAngle - Angle) * 2 * PI / 360));
  val2[1] = y1 + Math.round(L2 * Math.sin((xAngle - Angle) * 2 * PI / 360));
  return [val1, val2];
}

/** 获取线路径参数
 * @参数 StartPoint 开始点 [x,y]
 * @参数 EndPoint   结束点 [x,y]
 * @返回 LinePathInfo   中点 [x,y]
 */
export const onGetLinePath = (StartPoint: CoordinateType, EndPoint: CoordinateType): string => {
  let LinePathInfo = `M${StartPoint.x},${StartPoint.y}`;

  return LinePathInfo;
}

/** 求圆周上等分点的坐标
 * @圆心坐标cx
 * @圆心坐标cy
 * @半径r 
 * @等分个数Quantity
 * @返回_Points [{x,y},{x,y,}{x,y}]
*/
export const onGetCircleBisectionPoint = (
  cx: number/*圆心坐标CX*/,
  cy: number/*圆心坐标CY*/,
  radius: number /*半径 */,
  quantity: number /**分段数*/): any => {
 // console.log("✍ 计算函数-求圆周上等分点的坐标");
 // console.log(`cx==${cx};cy==${cy};r==${radius};count==${quantity};`);
  /** 路径点 */
  let pathPoints = [];
  let radians = (Math.PI / 180) * Math.round(360 / quantity); //弧度
  for (let i = 0; i < quantity; i++) {
    let X = cx + radius * Math.sin(radians * i);
    let Y = cy + radius * Math.cos(radians * i);
    pathPoints.push({ x: X, y: Y }); //为保持数据顺时针
  }
 // console.log(pathPoints);
  return pathPoints;
}

function linesIntersect(line1: any, line2: any) {
  // 获取线段1的两个端点
  const x1 = line1[0][0];
  const y1 = line1[0][1];
  const x2 = line1[1][0];
  const y2 = line1[1][1];

  // 获取线段2的两个端点
  const x3 = line2[0][0];
  const y3 = line2[0][1];
  const x4 = line2[1][0];
  const y4 = line2[1][1];

  // 计算向量
  const v1 = [(x4 - x3), (y4 - y3)];
  const v2 = [(x2 - x1), (y2 - y1)];
  const v3 = [(x3 - x1), (y3 - y1)];

  // 计算叉积
  const cross1 = v1[0] * v2[1] - v1[1] * v2[0];
  const cross2 = v3[0] * v2[1] - v3[1] * v2[0];

  // 如果叉积符号相反，则相交
  if (cross1 * cross2 < 0) {
    return true;
  }

  return false;
}
/** 获取两线交点
 * @参数 k1 开始点 x
 * @参数 b1 开始点 y
 * @参数 k2 结束点 x
 * @参数 b2 结束点 y
 * @返回 交点 [x,y]
 */
export const onGetIntersectionPoint = (k1: number, b1: number, k2: number, b2: number): number[] => {
  const x = (b2 - b1) / (k1 - k2)
  const y = k1 * x + b1
  return [x, y];
}


